package Frame;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileSystemView;

import Function.FCItemLibrary;
import Function.FCTankLibrary;
import VWorldGameStudio.FCTank;

public class FCEditTank extends JDialog {
	private JFileChooser filec = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory()) {
		protected javax.swing.JDialog createDialog(java.awt.Component parent) throws java.awt.HeadlessException {
			JDialog jd = super.createDialog(parent);
			jd.setSize(500, 400);
			jd.setTitle("选择文件");
			jd.setResizable(false);
			jd.setLocationRelativeTo(null);
			return jd;
		};
	};
	private FCTank editTank = null;
	private int editIndex = 0;
	public FCEditTank(boolean isnew, FCTankLibrary tankl, FCItemLibrary iteml) {
		setTitle((isnew) ? "坦克管理-新建坦克" : "坦克管理-编辑坦克,主动点击查看");
		setBounds(0, 0, 415, 480);
		setModal(true);
		setResizable(false);
		setLayout(null);
		editTank = null;
		setLocationRelativeTo(null);

		JLabel a = new JLabel("坦克ID");
		a.setBounds(10, 10, 60, 20);
		this.add(a);
		JComboBox<String> a1 = new JComboBox<String>();
		a1.setBounds(70, 10, 130, 20);
		a1.setEnabled(!isnew);

		if (isnew)
			a1.addItem(String.valueOf(tankl.getNewTankID()));
		else {
			for (int i = 0; i < tankl.getNewTankID(); i++) {
				a1.addItem(String.valueOf(i));
			}
		}
		this.add(a1);



		JLabel b = new JLabel("名    称");
		b.setBounds(210, 10, 60, 20);
		this.add(b);
		JTextField b1 = new JTextField();
		b1.setBounds(270, 10, 130, 20);
		this.add(b1);

		JLabel c = new JLabel("主    炮");
		c.setBounds(10, 40, 60, 20);
		this.add(c);
		JComboBox<String> c1 = new JComboBox<String>();
		c1.setBounds(70, 40, 130, 20);
		Vector<String> asd = iteml.getGun1ID();
		c1.addItem("null");
		for (int i = 0; i < asd.size(); i++)
			c1.addItem(asd.get(i));
		this.add(c1);
		JLabel c2 = new JLabel("副    炮");
		c2.setBounds(210, 40, 130, 20);
		this.add(c2);
		JComboBox<String> c3 = new JComboBox<String>();
		c3.setBounds(270, 40, 130, 20);
		asd = iteml.getGun2ID();
		c3.addItem("null");
		for (int i = 0; i < asd.size(); i++)
			c3.addItem(asd.get(i));
		this.add(c3);

		JLabel d = new JLabel("发动机");
		d.setBounds(10, 70, 60, 20);
		this.add(d);
		JComboBox<String> d1 = new JComboBox<String>();
		d1.setBounds(70, 70, 130, 20);
		asd = iteml.getEngineID();
		d1.addItem("null");
		for (int i = 0; i < asd.size(); i++)
			d1.addItem(asd.get(i));
		this.add(d1);
		JLabel d2 = new JLabel("C装置");
		d2.setBounds(210, 70, 60, 20);
		this.add(d2);
		JComboBox<String> d3 = new JComboBox<String>();
		d3.setBounds(270, 70, 130, 20);
		asd = iteml.getCdeviceID();
		d3.addItem("null");
		for (int i = 0; i < asd.size(); i++)
			d3.addItem(asd.get(i));
		this.add(d3);

		JLabel e = new JLabel("底    盘");
		e.setBounds(10, 100, 60, 20);
		this.add(e);
		JComboBox<String> e1 = new JComboBox<String>();
		e1.setBounds(70, 100, 130, 20);
		asd = iteml.getChassisID();
		e1.addItem("null");
		for (int i = 0; i < asd.size(); i++)
			e1.addItem(asd.get(i));
		this.add(e1);
		JLabel e2 = new JLabel("雷达装置");
		e2.setBounds(210, 100, 60, 20);
		this.add(e2);
		JComboBox<String> e3 = new JComboBox<String>();
		e3.setBounds(270, 100, 130, 20);
		asd = iteml.getRadarID();
		e3.addItem("null");
		for (int i = 0; i < asd.size(); i++)
			e3.addItem(asd.get(i));
		this.add(e3);

		JLabel e4 = new JLabel("S-E");
		e4.setBounds(210, 130, 60, 20);
		this.add(e4);
		JComboBox<String> e5 = new JComboBox<String>();
		e5.setBounds(270, 130, 130, 20);
		asd = iteml.getSEID();
		e5.addItem("null");
		for (int i = 0; i < asd.size(); i++)
			e5.addItem(asd.get(i));
		this.add(e5);

		JLabel f = new JLabel("身份材质");
		f.setBounds(10, 160, 60, 20);
		this.add(f);
		JTextField f1 = new JTextField();
		f1.setBounds(10, 190, 190, 20);
		f1.setEnabled(false);
		f1.setDisabledTextColor(Color.black);
		this.add(f1);
		JButton f2 = new JButton("选择");
		f2.setBounds(130, 160, 70, 20);
		this.add(f2);
		JLabel f3 = new JLabel("", JLabel.CENTER);
		f3.setBounds(10, 220, 190, 60);
		f3.setBorder(BorderFactory.createLineBorder(Color.black));
		this.add(f3);
		f2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (filec.showOpenDialog(null) == filec.APPROVE_OPTION)
					f1.setText(filec.getSelectedFile().getPath().replaceAll("\\\\", "/"));
				f3.setIcon(new ImageIcon(f1.getText()));
			}
		});

		JLabel h = new JLabel("纹路材质");
		h.setBounds(10, 290, 60, 20);
		this.add(h);
		JTextField h1 = new JTextField();
		h1.setBounds(10, 320, 190, 20);
		h1.setEnabled(false);
		h1.setDisabledTextColor(Color.black);
		this.add(h1);
		JButton h2 = new JButton("选择");
		h2.setBounds(130, 290, 70, 20);
		this.add(h2);
		JLabel h3 = new JLabel("", JLabel.CENTER);
		h3.setBounds(10, 350, 190, 60);
		h3.setBorder(BorderFactory.createLineBorder(Color.black));
		h3.setBackground(Color.black);
		h3.setOpaque(true);
		this.add(h3);
		h2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (filec.showOpenDialog(null) == filec.APPROVE_OPTION)
					h1.setText(filec.getSelectedFile().getPath().replaceAll("\\\\", "/"));
				h3.setIcon(new ImageIcon(h1.getText()));
			}
		});

		JLabel g = new JLabel("行走材质");
		g.setBounds(210, 160, 60, 20);
		this.add(g);
		JTextField g1 = new JTextField();
		g1.setBounds(210, 190, 190, 20);
		g1.setEnabled(false);
		g1.setDisabledTextColor(Color.black);
		this.add(g1);
		JButton g2 = new JButton("选择");
		g2.setBounds(330, 160, 70, 20);
		this.add(g2);
		JLabel g3 = new JLabel("", JLabel.CENTER);
		g3.setBounds(210, 220, 190, 190);
		g3.setBorder(BorderFactory.createLineBorder(Color.black));
		this.add(g3);
		g2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (filec.showOpenDialog(null) == filec.APPROVE_OPTION)
					g1.setText(filec.getSelectedFile().getPath().replaceAll("\\\\", "/"));
				g3.setIcon(new ImageIcon(g1.getText()));
			}
		});

		JButton ok = new JButton("确认");
		ok.setBounds(10, 420, 390, 18);
		this.add(ok);

		ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (b1.getText() != null && !b1.getText().equals("")) {
					if (f1.getText() != null && !f1.getText().equals("")) {
						if (g1.getText() != null && !g1.getText().equals("")) {
							if (!isnew || !tankl.isTankRepeat(b1.getText())) {
								tankl.registerTanks(isnew, b1.getText(), c1.getSelectedItem().toString(),
										c3.getSelectedItem().toString(), e5.getSelectedItem().toString(),
										d1.getSelectedItem().toString(), d3.getSelectedItem().toString(), e1.getSelectedItem().toString(),
										e3.getSelectedItem().toString(), f1.getText(), g1.getText(), h1.getText(), editIndex);
								new FCTip("坦克编辑", "设置成功!");
								dispose();
							} else
								new FCTip("坦克编辑", "名称重复!");
						} else
							new FCTip("坦克编辑", "行走素材不得为空!");
					} else
						new FCTip("坦克编辑", "身份素材不得为空!");
				} else
					new FCTip("坦克编辑", "名称不得为空!");
			}
		});

		a1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				editTank = tankl.getTankOfNum(a1.getSelectedIndex());
				if(editTank != null){
					editIndex = a1.getSelectedIndex();
					System.out.println("editTank "+editTank.toString());
					b1.setText(editTank.getName());
					c1.setSelectedItem(editTank.getGunID());
					c3.setSelectedItem(editTank.getGun2ID());
					d1.setSelectedItem(editTank.getEngineID());
					d3.setSelectedItem(editTank.getCdeviceID());
					e1.setSelectedItem(editTank.getChassisID());
					e3.setSelectedItem(editTank.getRadarID());
					e5.setSelectedItem(editTank.getSEID());
					f1.setText(editTank.getImageMasterUrl());
					h1.setText(editTank.getImageWhiteLineUrl());
					g1.setText(editTank.getImageDirUrl());
				}
			}
		});
		setVisible(true);
	}
}
